<template>
  <div class="user">
    <div class="search">
      <PageSearch
        :searchFormConfig="searchFormConfig"
        @resetBtnClick="handleResetClick"
        @queryBtnClick="handleQueryClick"
      />
    </div>
    <div>
      <page-content
        ref="pageContentRef"
        :contentTableConfig="contentTableConfig"
        page-name="users"
        @newBtnClick="handleNewData"
        @editBtnClick="handleEditData"
      ></page-content>
    </div>
    <pageModal
      ref="pageModalRef"
      :defaultInfo="defaultInfo"
      :modalConfig="modalConfigRef"
      pageName="users"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from '@/store'

import PageSearch from '@/components/page-search/index'
import PageContent from '@/components/page-content'
import pageModal from '@/components/page-modal'

import { searchFormConfig } from './config/search.config'
import { contentTableConfig } from './config/content.config'
import { modalConfig } from './config/modal.config'

import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModal } from '@/hooks/userPageModal'
export default defineComponent({
  components: { PageSearch, PageContent, pageModal },
  name: 'users',
  setup() {
    const [pageContentRef, handleResetClick, handleQueryClick] = usePageSearch()

    // pageModal 相关的hook 逻辑
    // 处理密码逻辑
    const newCallback = () => {
      const passwordItem = modalConfig.formItems.find((item) => item.field == 'password')
      passwordItem!.isHidden = false
    }
    const editCallback = () => {
      const passwordItem = modalConfig.formItems.find((item) => item.field == 'password')
      passwordItem!.isHidden = true
    }
    // 2.动态添加部门和角色列表
    const store = useStore()
    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find((item) => item.field == 'departmentId')
      departmentItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })

      const roleItem = modalConfig.formItems.find((item) => item.field == 'roleId')
      roleItem!.options = store.state.entirerRole.map((item) => {
        return { title: item.name, value: item.id }
      })
      return modalConfig
    })

    // 3. 调用hook获取公共变量和函数
    const [pageModalRef, defaultInfo, handleNewData, handleEditData] = usePageModal(
      newCallback,
      editCallback
    )

    return {
      searchFormConfig,
      contentTableConfig,
      pageContentRef,
      // modalConfig,
      // userList,
      handleResetClick,
      handleQueryClick,
      handleNewData,
      handleEditData,
      pageModalRef,
      defaultInfo,
      modalConfigRef
    }
  }
})
</script>

<style scoped lang="less"></style>
